<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <link href="{{url_for('static', filename='css/bootstrap.min.css')}}" rel="stylesheet">
    <link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
    <title>显示词云</title>
    <style>
        .my-h1 {
            margin-bottom: 3rem!important;
            margin-top: 2rem!important;
            font-size: 42px;
            text-align: center;
        }
        </style>
</head>
<body>
    <div class="container">
        <h1 class="my-h1">显示词云</h1>
        <form id="myForm" method="post">
            <div class="row">
                <div class="col-md-3 form-group">
                    <select class="form-control" id="grade" name="grade" onchange="updateClassOptions()">
                        <option value="">请选择年级</option>
                        {% for grade in grade_options %}
                            <option value="{{ grade }}" {% if grade == selected_grade %}selected{% endif %}>{{ grade }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-3 form-group">
                    <select class="form-control" id="lesson" name="lesson" onchange="updateClassOptions()">
                        <option value="">请选择课程</option>
                        {% for lesson in lesson_options %}
                            <option value="{{ lesson }}" {% if lesson == selected_lesson %}selected{% endif %}>{{ lesson }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-3 form-group">
                    <select class="form-control" id="class_n" name="class_n">
                        <option value="">请选择班级（非必选）</option>
                        {% for class_n in class_n_options %}
                            <option value="{{ class_n }}" {% if class_n == selected_class_n %}selected{% endif %}>{{ class_n }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-3 form-group">
                    <button type="submit" class="btn btn-primary">查询</button>
                </div>
            </div>
        </form>
        <hr>
        <div class="row">
            <div class="col-md-8">
                {% if wordcloud %}
                    <img src="data:image/png;base64,{{ wordcloud }}">
                {% else %}
                    <p>暂无数据</p>
                {% endif %}
            </div>
            <div class="col-md-4">
                {% if top5_words %}
                    <ul class="list-group">
                    {% for word, count in top5_words %}
                    <li class="list-group-item list-group-item-action list-group-item-info">{{ word }}: {{ count }}</li>
                    {% endfor %}
                    </ul>
                {% endif %}
            </div>
        </div>
    </div>

    <script>
        document.getElementById('myForm').addEventListener('submit', function(e) {
            var grade = document.getElementById('grade').value;
            var lesson = document.getElementById('lesson').value;
            if (grade === '' || lesson === '') {
                alert('请选择年级和第几课');
                e.preventDefault();
            }
        });
        function updateClassOptions() {
            var grade = document.getElementById('grade').value;
            var lesson = document.getElementById('lesson').value;

            // 检查 grade 和 lesson 是否都已经选择
            if (!grade || !lesson) {
                return;
            }

            // 使用 AJAX 向服务器发送请求
            var xhr = new XMLHttpRequest();
            xhr.open('GET', '/get_class_options?grade=' + grade + '&lesson=' + lesson, true);
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var classOptions = JSON.parse(JSON.parse(xhr.responseText)); // ["501", "502"]
                    // console.log(classOptions); // 打印转换后的 JavaScript 对象
                    // console.log(typeof classOptions);  // 打印 classOptions 的类型
                    // console.log(Array.isArray(classOptions));  // 检查 classOptions 是否为数组

                    // 更新班级选择框
                    var classSelect = document.getElementById('class_n');
                    classSelect.innerHTML = '';

                    // 创建并添加默认的选项
                    var defaultOption = document.createElement('option');
                    defaultOption.value = '';
                    defaultOption.text = '请选择班级（非必选）';
                    classSelect.appendChild(defaultOption);

                    for (var i = 0; i < classOptions.length; i++) {
                        var option = document.createElement('option');
                        option.value = classOptions[i];
                        option.text = classOptions[i];
                        classSelect.appendChild(option);
                    }
                }
            };
            xhr.send();
        }
    </script>
</body>
</html>